# -*- coding: utf-8 -*-
'''
@文件    :funds.py
@说明    :资金账户操作接口
@时间    :2025/08/02 02:46:43
@作者    :浪晋
@版本    :1.0
'''

from . import base
from .. import utils


class FundsClient(base.BaseClient):
    def delayed_split_apply(self, originalId, fromMerchantNo, productCode, toMerchantNo, amount):
        """申请分账"""
        url = "https://pay.trx.helipay.com"  + "/trx/delayedSplit/apply"
        headers = {
            "Content-Type": "application/json;charset=utf-8"
        }
        body = {
            "orderId": utils.parameter.generate_order_no("ds"),
            "originalOrderId": originalId,  # 原始订单号(就是自己生成的那个)
            "originalProductCode": productCode,  # 产品类型编码  APPPAY、QUICKPAY
            "ruleJson": [
                {"splitBillMerchantNo": toMerchantNo, "splitBillAmount": amount}
            ]
        }
        data = self._build_delayedSplit_data(fromMerchantNo, body)
        return self._request(url, headers=headers, data=data)


    def query_merchant_account(self, merchantNo):
        """商户余额查询"""
        url = self.base_url + "/trx/merchant/interface.action"
        headers = {
            "Content-Type":"application/x-www-form-urlencoded;charset=utf-8"
        }
        body = {
            "P2_customerNumber": merchantNo,
            "P3_timestamp": utils.parameter.generate_timestamp()
        }
        data = self._build_common_data("MerchantAccountQuery", body)
        return self._request(url, headers=headers, data=data, verified="sign")


    def settlement_merchant(self, merchantNo, amount: float, summary: str, notifyUrl: str):
        """商户结算接口"""
        url = self.base_url + "/trx/transfer/interface.action"
        headers = {
            "Content-Type":"application/x-www-form-urlencoded;charset=utf-8"
        }
        body = {
            "P2_orderId": utils.parameter.generate_order_no("transfer"),
            "P3_customerNumber": merchantNo,
            "P4_amount": amount,
            "P5_summary": summary,
            "P6_notifyUrl": notifyUrl
        }
        data = self._build_transfer_data("MerchantSettlement", body)
        return self._request(url, headers=headers, data=data, verified="sign")
    

    def query_settlement_merchant(self, orderId, merchantNo, settleDate):
        """查询商户结算接口"""
        url = self.base_url + "/trx/transfer/interface.action"
        headers = {
            "Content-Type":"application/x-www-form-urlencoded;charset=utf-8"
        }
        body = {
            "P2_orderId": orderId,
            "P3_customerNumber": merchantNo,
            "P4_settleDate": settleDate
        }
        data = self._build_transfer_data("MerchantSettlementQuery", body)
        return self._request(url, headers=headers, data=data, verified="sign")